package com.whitedust.top20;

import java.util.Stack;

/**
 * @author A
 * @since 2025/11/3 15:27
 */

public class Top17 {
    public boolean isValid(String s) {
        //栈的最经典场景，匹配
        Stack<Character> st = new Stack<>();
        char[] charArray = s.toCharArray();
        for (char c : charArray) {
            if (st.isEmpty()) {
                st.push(c);
                continue;
            }
            char top = st.peek();
            if ((top == '(' && c == ')') || (top == '[' && c == ']') || (top == '{' && c == '}')) st.pop();
            else st.push(c);
        }
        return st.isEmpty();
    }
}
